package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.About;
import cn.xbtop.blog.service.AboutService;
import cn.xbtop.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/14 9:12
 */
@RestController
@RequestMapping("/about")
public class AboutController {

    @Autowired
    private AboutService aboutService;

    /**
     * 添加
     * @param about
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody About about) {
        aboutService.insert(about);
        return new Result<>("添加成功！");
    }

    /**
     * 修改
     * @param about
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody About about) {
        aboutService.updateByPrimaryKey(about);
        return new Result<>("修改成功！");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Result<Object> findById(@PathVariable Integer id) {
        About about = aboutService.selectByPrimaryKey(id);
        return new Result<>(about);
    }

    /**
     * 保证只存在一个
     * @return
     */
    @RequestMapping(value = "/read", method = RequestMethod.GET)
    public Result<Object> read() {
        About about = aboutService.read();
        return new Result<>(about);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result<Object> delete(@PathVariable Integer id) {
        aboutService.deleteByPrimaryKey(id);
        return new Result<>("删除成功");
    }

    /**
     * 启用
     * @param id
     * @return
     */
    @RequestMapping(value = "/enableById/{id}", method = RequestMethod.PUT)
    public Result<Object> enableById(@PathVariable Integer id) {
        aboutService.enableById(id);
        return new Result<>("启用成功");
    }

    /**
     * 弃用
     * @param id
     * @return
     */
    @RequestMapping(value = "/disEnableById/{id}", method = RequestMethod.PUT)
    public Result<Object> disEnableById(@PathVariable Integer id) {
        aboutService.disEnableById(id);
        return new Result<>("弃用成功");
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<Object> list() {
        List<About> list = aboutService.list();
        return new Result<>(list);
    }

}
